﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media;

namespace WpfMvvmTemplate.HC.Common
{
    public class ComMethod
    {
        /// <summary>
        /// 中英文切换
        /// </summary>
        /// <param name="languageNum">语言编号：0-中文；1-英文</param>
        /// <param name="window">所在的窗体</param>
        /// <param name="languageDic">中英文字典</param>
        public static void LanguageWinDicHelper(int languageNum, Window window, bool isLoadSoure = true)
        {
            string LanguageFile0 = "./Resources/Languages/chinese.xaml";
            string LanguageFile1 = "./Resources/Languages/english.xaml";

            ResourceDictionary rd = new()
            {
                Source = languageNum == 0
                    ? new Uri(LanguageFile0, UriKind.RelativeOrAbsolute)
                    : new Uri(LanguageFile1, UriKind.RelativeOrAbsolute)
            };

            if(isLoadSoure)
            {
                // 加载窗体window
                window.Resources?.MergedDictionaries?.Add(rd);
            }

            // 加载后台字典dic
            Dictionary<string, string> languageDic = new();

            foreach(DictionaryEntry item in rd)
            {
                languageDic.Add(item.Key.ToString() ?? "", item.Value?.ToString() ?? "");
            }

            GlobalStaticData.LanguageDic = languageDic;
        }

        /// <summary>
        /// 格式化json字符串
        /// </summary>
        /// <param name="str">输入</param>
        /// <returns></returns>
        public static string ConvertJsonString(string str)
        {
            Newtonsoft.Json.JsonSerializer serializer = new();
            TextReader tr = new StringReader(str);
            Newtonsoft.Json.JsonTextReader jtr = new(tr);

            object? obj = serializer.Deserialize(jtr);
            if(obj != null)
            {
                StringWriter textWriter = new StringWriter();
                Newtonsoft.Json.JsonTextWriter jsonWriter = new(textWriter)
                {
                    Formatting = Newtonsoft.Json.Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ' '
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            }
            else
            {
                return str;
            }
        }

        /// <summary>
        /// 克隆对象，深拷贝
        /// 原对象属性值变化后该对象不会变化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static T DeepCopy<T>(T t)
        {
            //使用System.Text.Json做深拷贝可行，
            //使用Newtonsoft.Json，其中的ObservableCollection<T> 集合元素会翻倍
            return System.Text.Json.JsonSerializer.Deserialize<T>(System.Text.Json.JsonSerializer.Serialize(t))!;
        }

        /// <summary>
        /// 使用VisualTreeHelper查找指定类型的子元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T? FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            for(int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject? child = VisualTreeHelper.GetChild(obj, i);
                if(child != null && child is T)
                    return (T)child;
                else
                {
                    T? childOfChild = FindVisualChild<T>(child!);
                    if(childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
    }
}